home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / common / sqrt.c < prev    next >
Text File  |  1996-07-24  |  572b  |  30 lines

  1. /*
  2.  * static char *rcsid_sqrt_c =
  3.  *   "$Id: sqrt.c,v 1.11 1994/03/29 08:28:26 master Exp $";
  4.  */
  5.  
  6. /*
  7.  * Based on (n+1)^2 = n^2 + 2n + 1
  8.  * given that    1^2 = 1, then
  9.  *        2^2 = 1 + (2 + 1) = 1 + 3 = 4
  10.  *         3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9
  11.  *         4^2 = 9 + (6 + 1) = 9 + 7 = 1 + 3 + 5 + 7 = 16
  12.  *        ...
  13.  * In other words, a square number can be express as the sum of the
  14.  * series n^2 = 1 + 3 + ... + (2n-1)
  15.  */
  16. int
  17. isqrt(n)
  18. int n;
  19. {
  20.     int result, sum, prev;
  21.     result = 0;
  22.     prev = sum = 1;
  23.     while (sum <= n) {
  24.         prev += 2;
  25.         sum += prev;
  26.         ++result;
  27.     }
  28.     return result;
  29. }
  30.